package com.example.inoculations.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.inoculations.entity.Inoculation;
import com.example.inoculations.entity.Reservation;
import com.example.inoculations.entity.User;
import com.example.inoculations.entity.Vaccin;
import com.example.inoculations.resultR.R;
import com.example.inoculations.service.IInoculationService;
import com.example.inoculations.service.IUserService;
import com.example.inoculations.service.IVaccinService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.stereotype.Controller;

import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author meirui
 * @since 2022-11-19
 */
@Controller
@ResponseBody
@RequestMapping("/inoculations/inoculation")
public class InoculationController {
    @Autowired
    IInoculationService iInoculationService;
    @Autowired
    IUserService iUserService;
    @Autowired
    IVaccinService iVaccinService;
    /*分页查询*/
    @GetMapping("/pageall")
    public R<IPage<Inoculation>> getpageall(@RequestParam("pageNum") Integer pageNum,
                                            @RequestParam("pageSize") Integer pageSize,
                                            @RequestParam(required = false) String userid,
                                            @RequestParam(required = false) String vaccinid) {
        IPage<Inoculation> page = new Page<>(pageNum,pageSize);
        QueryWrapper<Inoculation> queryWrapper = new QueryWrapper<>();
        /*根据身份证号模糊查询*/
        if (!StringUtils.isEmpty(userid)){
            queryWrapper.like("userid",userid);
        }
        /*根据标题模糊查询*/
        if (!StringUtils.isEmpty(vaccinid)){
            queryWrapper.like("vaccinid",vaccinid);
        }
        queryWrapper.orderByDesc("id");//根据id倒序
        IPage<Inoculation> page1 = iInoculationService.page(page, queryWrapper);
        //根据id查询对应的用户表和疫苗表信息
        List<Inoculation> records = page1.getRecords();
        for(Inoculation it: records){
            String idcard = it.getUserid();
            if(idcard!=null)
            {
                QueryWrapper<User> qw = new QueryWrapper();
                qw.eq("idcard",idcard);
                User user = iUserService.getOne(qw);
                if (user!=null) {
                    it.setUser(user);
                }
            }
            String createid = it.getVaccinid();
            if (createid!=null) {
                QueryWrapper<Vaccin> qw = new QueryWrapper();
                qw.eq("createid",createid);
                Vaccin vaccin = iVaccinService.getOne(qw);
                if (vaccin!=null) {
                    it.setVaccin(vaccin);
                }
            }
        }

        return R.SUCCESS(page1);
    }
    /*添加和修改，根据是否传入id，有则修改，无则添加*/
    @PostMapping("/save")
    public R save(@RequestBody Inoculation inoculation){
        if (iInoculationService.saveOrUpdate(inoculation)){
            return R.SUCCESS();
        }else {
            return R.FAIL();
        }
    }
    /*删除*/
    @DeleteMapping("/del/{id}")
    public R del(@PathVariable Integer id){
        if (iInoculationService.removeById(id)){
            return R.SUCCESS();
        }else {
            return R.FAIL();
        }
    }
}
